package com.nbcio.modules.erp.party.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbcio.common.core.domain.R;
import com.nbcio.modules.erp.party.domain.ErpSupplier;
import com.nbcio.modules.erp.party.domain.ErpCustomer;
import com.nbcio.modules.erp.party.mapper.ErpSupplierMapper;
import com.nbcio.modules.erp.party.mapper.ErpCustomerMapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "往来方")
@RestController
@RequestMapping("/erp/party")
@RequiredArgsConstructor
public class ErpPartyController {

    private final ErpSupplierMapper supplierMapper;
    private final ErpCustomerMapper customerMapper;

    // ========== 供应商接口 ==========
    
    @GetMapping("/supplier/page")
    public R<IPage<ErpSupplier>> supplierPage(@RequestParam int pageNum,
                                              @RequestParam int pageSize,
                                              @RequestParam(required = false) String code,
                                              @RequestParam(required = false) String name,
                                              @RequestParam(required = false) Integer status) {
        LambdaQueryWrapper<ErpSupplier> qw = new LambdaQueryWrapper<>();
        if (code != null && !code.isEmpty()) qw.like(ErpSupplier::getSupplierCode, code);
        if (name != null && !name.isEmpty()) qw.like(ErpSupplier::getSupplierName, name);
        if (status != null) qw.eq(ErpSupplier::getStatus, status);
        qw.orderByDesc(ErpSupplier::getCreateTime);
        return R.ok(supplierMapper.selectPage(Page.of(pageNum, pageSize), qw));
    }

    @GetMapping("/supplier/{id}")
    public R<ErpSupplier> getSupplier(@PathVariable("id") String id) {
        return R.ok(supplierMapper.selectById(Long.parseLong(id)));
    }

    @PostMapping("/supplier")
    public R<Boolean> createSupplier(@RequestBody ErpSupplier supplier) {
        return R.ok(supplierMapper.insert(supplier) > 0);
    }

    @PutMapping("/supplier/{id}")
    public R<Boolean> updateSupplier(@PathVariable("id") String id, @RequestBody ErpSupplier supplier) {
        supplier.setSupplierId(Long.parseLong(id));
        return R.ok(supplierMapper.updateById(supplier) > 0);
    }

    @DeleteMapping("/supplier/{id}")
    public R<Boolean> deleteSupplier(@PathVariable("id") String id) {
        return R.ok(supplierMapper.deleteById(Long.parseLong(id)) > 0);
    }

    @PostMapping("/supplier/batch-delete")
    public R<Boolean> batchDeleteSupplier(@RequestBody List<String> ids) {
        List<Long> longIds = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return R.ok(supplierMapper.deleteBatchIds(longIds) > 0);
    }

    // ========== 客户接口 ==========

    @GetMapping("/customer/page")
    public R<IPage<ErpCustomer>> customerPage(@RequestParam int pageNum,
                                              @RequestParam int pageSize,
                                              @RequestParam(required = false) String code,
                                              @RequestParam(required = false) String name,
                                              @RequestParam(required = false) Integer status) {
        LambdaQueryWrapper<ErpCustomer> qw = new LambdaQueryWrapper<>();
        if (code != null && !code.isEmpty()) qw.like(ErpCustomer::getCustomerCode, code);
        if (name != null && !name.isEmpty()) qw.like(ErpCustomer::getCustomerName, name);
        if (status != null) qw.eq(ErpCustomer::getStatus, status);
        qw.orderByDesc(ErpCustomer::getCreateTime);
        return R.ok(customerMapper.selectPage(Page.of(pageNum, pageSize), qw));
    }

    @GetMapping("/customer/{id}")
    public R<ErpCustomer> getCustomer(@PathVariable("id") String id) {
        return R.ok(customerMapper.selectById(Long.parseLong(id)));
    }

    @PostMapping("/customer")
    public R<Boolean> createCustomer(@RequestBody ErpCustomer customer) {
        return R.ok(customerMapper.insert(customer) > 0);
    }

    @PutMapping("/customer/{id}")
    public R<Boolean> updateCustomer(@PathVariable("id") String id, @RequestBody ErpCustomer customer) {
        customer.setCustomerId(Long.parseLong(id));
        return R.ok(customerMapper.updateById(customer) > 0);
    }

    @DeleteMapping("/customer/{id}")
    public R<Boolean> deleteCustomer(@PathVariable("id") String id) {
        return R.ok(customerMapper.deleteById(Long.parseLong(id)) > 0);
    }

    @PostMapping("/customer/batch-delete")
    public R<Boolean> batchDeleteCustomer(@RequestBody List<String> ids) {
        List<Long> longIds = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return R.ok(customerMapper.deleteBatchIds(longIds) > 0);
    }
}


